const BaseRest = require('../rest.js');

module.exports = class extends BaseRest {
  async menuTreeAction() {
    const menuList = await this.modelInstance.select();
    return this.success(this.formatTree(menuList, 'id'))
  }

  async roleMenuAction() {
    const {roleId} = this.get()
    const menuList = await this.modelInstance.select();
    const role = await this.model('role').where({id: roleId}).field('id,menu_list').find()
    let menuIds = role.menu_list
    menuIds += ','
    for (let index = 0; index < menuList.length; index++) {
      let menu = menuList[index]
      menu.checked = false
      menu.open = true
      menu.leaf = true
      menu.visible = true
      menu.parentId =menu.parent_id
      menu.key = menu.parent_path.replace('/', '-') + menu.id
      if (menuIds.indexOf(menu.id + ',') >= 0) {
        menu.checked = true
      }
    }
    return this.success(this.formatTree(menuList))
  }
};
